/*
*	mgFargo-virtsInHandler.e
*	Version 1.00
*
*	This source file is the source of a compiled macro that runs under
*	the Epsilon (see note) text editor.
*
*	Incorporating the macro into Epsilon:
*	-------------------------------------
*	1. Read this file into Epsilon (load it like any other ordinary file)
*	2. Run the command 'compile-buffer'.
*	3. Run the command 'write-state' so that the compiled buffer is made part
*		of your epsilon.sta file and will be automatically available next
*		time epsilon loads.
*
*   Note: "Epsilon" is a registered trademark licensed to Lugaru Software, Ltd. 
*	"EEL" and "Lugaru" are trademarks of Lugaru Software, Ltd.	
*
*	Using the macros:
*	----------------
	mgFargo_findVirtsInHandler.

	Run from a grep buffer of the results of the RE Search ^Operations: Handler
	While not at end of grep buffer.
		Go to next item in grep buffer
		Go to the file.
		Search for Task Name:
			If not found, go to end of file.
		Narrow selection
		Search for <tab>Virt:
		If found, write file name, handler, virtual to buffer x.
		widen selection.
		return to grep buffer.
	Look for results in buffer X.
*
*	Created:
*		2010-1209 Ira Gershenhorn 
*	
*	Updated:
*	
*        
*/
#include "eel.h"
int mark1;


// on entry mark1 is at the end of the "e.uat " string
command mgFargo_findVirtsInHandler()		
{
	int linecounter = 0;
	char handlerName[100];
	char progName[100];
	int grepbufnum;
	int i=0, found, nextHandler;
	int headerOutput=0;

	if( strcmp(bufname,"grep") ){
		say ("You must start in the grep buffer for this macro to work. This is the %s buffer", bufname);
		return;
	}
	grepbufnum = bufnum;
	while (1){
		char *initials;
		char grepLine[500];

		//say( "Starting" );
		to_begin_line();
		//say( "Searching txt to see if done" );
		if( !do_searching( 0, ".txt:") ){
			say( "done - no more lines" );
			break;//if nothing found, then we are done.
		}
		linecounter++;
		//say("1: %d", point);
		to_begin_line();
		//say( "Searching txt to grab program Name" );
		if( !re_search (RE_SHORTEST, "\z.*%.txt") ){
			say( "done - no more programs" ); // should always be true
			break;//if nothing found, then we are done.
		}
		grab( matchstart+strlen("\z"), matchend - strlen(".txt"), progName);
		//say( "Searching Handler and Directive to grab handler Name" );
		if( !re_search (0, "Handler.+Directive:") ){
			say( "done - no more handlers" ); // should always be true
			break;//if nothing found, then we are done.
		}
		grab( matchstart + strlen("Handler "), matchend - strlen("Directive:"), handlerName);
		//say( "Diving in" );
		do_grep_examine(NULL);// dive into the grepped file
		nl_forward();
		point = matchend;
		mark1 = point;
		//say( "searching next Handler" );
		nextHandler = matchstart = 0;
		if( re_search (0, "^Operations:") ){// This could fail if there are no tasks after the handler
			nextHandler=matchstart;
			//say( "next Operations found" );
		}
		//say( "searching Task Name" );
		point = mark1; // return to starting point of search
		if( !re_search (0, "Task Name:") ){// This could fail if there are no tasks after the handler
			matchstart = 0;
			point = size()-1;
			if( nextHandler != 0)
				point = nextHandler;
		}
		else {
			point = matchstart;
			if( nextHandler != 0 && nextHandler < matchstart){
				point = nextHandler;
			}
		}
		//say ( "point, handlerName %s, nextHandler %d, matchstart %d ", handlerName, nextHandler, matchstart);
		mark = mark1;
		narrow_to_region();
		found = re_search (0, "\tVirt:");// This could fail if there are no virtuals defined
		widen_buffer();
		if( found ){
			to_buffer( "x" ); // change to other buffer to store result.
			if( !headerOutput){
				headerOutput = 1;
				bprintf( "%s\t%s\n", "Program", "Handler" );
			}
			bprintf( "%s\t%s\n", progName, handlerName );
			note( "%d:%s", linecounter, progName );
		}
		//say( "Diving out" );
		to_buffer_num(grepbufnum);//back to starting point
		//say("3: %d", point);
		to_begin_line();
		nl_forward();
		//say("4: %d", point);
	}
	to_buffer( "x" );
}

